package com.jingdianjichi.auth.domain.service.impl;

import com.jingdianjichi.auth.common.enums.IsDeletedFlagEnum;
import com.jingdianjichi.auth.domain.entity.AuthRolePermissionBO;
import com.jingdianjichi.auth.domain.service.AuthRolePermissionDomainService;
import com.jingdianjichi.auth.infra.basic.entity.AuthRolePermission;
import com.jingdianjichi.auth.infra.basic.service.AuthRolePermissionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class AuthRolePermissionDomainServiceImpl implements AuthRolePermissionDomainService {

    @Resource
    private AuthRolePermissionService rolePermissionService;

    @Override
    public Boolean add(AuthRolePermissionBO rolePermissionBO) {
        Long roleId = rolePermissionBO.getRoleId();

        //传统的插入方式,这样就是多次交互了,我们去批量插入亲亲
        /*
            rolePermissionBO.getPermissionIdList().forEach(permissionId ->{
                AuthRolePermission rolePermission = new AuthRolePermission();
                rolePermission.setRoleId(roleId);
                rolePermission.setPermissionId(permissionId);
                rolePermission.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());

                rolePermissionService.insert(rolePermission);
            });
         */

        List<AuthRolePermission> rolePermissionList=new ArrayList<>();

        rolePermissionBO.getPermissionIdList().forEach(permissionId ->{
            AuthRolePermission rolePermission = new AuthRolePermission();
            rolePermission.setRoleId(roleId);
            rolePermission.setPermissionId(permissionId);
            rolePermission.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());

            rolePermissionList.add(rolePermission);
        });

        int count=rolePermissionService.batchInsert(rolePermissionList);

        return count>0;
    }
}
